package dgopenai

import (
	"strings"
)

// formatLlmOutput formats the output from an LLM by trimming whitespace and removing markdown code blocks
func formatLlmOutput(output string) string {
	output = strings.TrimSpace(output)
	if output == "" {
		return ""
	}

	if strings.Contains(output, "```json") {
		output = output[strings.LastIndex(output, "```json")+7:]
	}
	if strings.Contains(output, "```") {
		output = output[:strings.LastIndex(output, "```")]
	}
	output = strings.ReplaceAll(output, "\x00", "")

	return strings.TrimSpace(output)
}
